let nodeTypes = require('./nodeTypes');
function evaluate(node){
  let result=0;
  switch(node.type){
      // 第一个是默认的，所以肯定会进入第一个case
    case nodeTypes.Program:
        for(let child of node.children){
            result = evaluate(child);//child  Additive
        }
        break;
    
    /**
     * 将其子节点不断的递归，直到是Numeric 数字，开始计算
     */
    case nodeTypes.Additive:// + 
        result = evaluate(node.children[0])+evaluate(node.children[1]);
        break;   
    case nodeTypes.Minus:// -
        result = evaluate(node.children[0])-evaluate(node.children[1]);
        break;      
    case nodeTypes.Multiplicative:// *
        result = evaluate(node.children[0])*evaluate(node.children[1]);
        break;    
    case nodeTypes.Divide:// /
        result = evaluate(node.children[0])/evaluate(node.children[1]);
        break;          
    case nodeTypes.Numeric:
        result = parseFloat(node.value);
    default:
        break;        
  }  
  return result;
}
module.exports = evaluate;